{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Partial updates\n",
    "\n",
    "This example demonstrates how to perform partial updates on individual components.\n",
    "To learn more about this concept check [this doc page](https://rerun.io/docs/howto/logging/send-partial-updates).\n",
    "\n",
    "## Setup a point cloud"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import annotations\n",
    "\n",
    "import numpy as np\n",
    "import rerun as rr\n",
    "\n",
    "rng = np.random.default_rng(42)\n",
    "n = 1000\n",
    "positions = rng.uniform(0, 10, size=[n, 3])\n",
    "\n",
    "rr.init(\"partial_updates\")\n",
    "rr.set_time(\"t\", sequence=0)\n",
    "rr.log(\"points\", rr.Points3D(positions, radii=0.1))\n",
    "\n",
    "rr.notebook_show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "## Update colors for different timestamps in a loop\n",
    "The previously logged positions are still used"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10):\n",
    "    rr.set_time(\"t\", sequence=i)\n",
    "    colors_slide_x = n * [(255, 0, 0)] * (positions < i)\n",
    "    rr.log(\"points\", rr.Points3D.from_fields(colors=colors_slide_x))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "## Update colors for different timestamps in a single call\n",
    "Like before, but passes columnar data with a single SDK call"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "times = np.arange(0, 10)\n",
    "all_colors_slide_z = np.vstack([n * [(0, 0, 255)] * (positions < t) for t in times])\n",
    "\n",
    "rr.send_columns(\n",
    "    \"points\",\n",
    "    [rr.TimeColumn(\"t\", sequence=times)],\n",
    "    rr.Points3D.columns(colors=all_colors_slide_z).partition([n] * 10),\n",
    ")"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
